package com.kcnet.qk.controller.group;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.constant.Const;
import com.kcnet.qk.constant.StringConst;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.FBStringUtil;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;

import org.greenrobot.eventbus.EventBus;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jesse.nativelogger.NLogger;

import static com.kcnet.qk.constant.Const.LIST_VIEW;

/**
 * @Auther: 赵浩
 * @Date: 2018-04-27 15:34
 * @Description: 小组管理
 */
public class TeamTeamMagCTR extends BasePeopleController {
    private static TeamTeamMagCTR releaseimgctr;
    private static String TAG = "小组管理";


    private boolean isClickSeatch;

    private boolean isHorView;
    private boolean isSearchHomeList; //是否进入了搜索主页列表


    private boolean isGroupJoinList;
    private boolean isClickJoinGroupDetailJoin;


    private boolean look = false;


    private String content;
    private int runType;

    protected Map<String, Boolean> mRecordMap = Collections.synchronizedMap(new HashMap());


    public static TeamTeamMagCTR getInstance() {
        if (releaseimgctr == null) {
            synchronized (TeamTeamMagCTR.class) {
                if (releaseimgctr == null) {
                    releaseimgctr = new TeamTeamMagCTR();
                }
            }
        }
        return releaseimgctr;
    }

    @Override
    public void doTask(AccessibilityEvent event) {
        mClassName = (String) event.getClassName();
        mType = event.getEventType();


         toTaskReqAdd();



    }


    /**
     * 申请进入小组
     */
    public void toTaskReqAdd() {
        if (!initFbMain && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBLauncherUI)) {
            NLogger.d(TAG, "1、点击搜索框");
            sleepTimedDefault();
            RootShell.click(81,38);
           // Onclic_shell("81", "38");
            isClickSeatch = true;
            initFbMain = true;
        } else if (isClickSeatch && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.katana.activity.ImmersiveActivity")) {
            NLogger.d(TAG, "2、事件 点击搜索框-输入主页名称");
            sleepTimedDefault();
            inputSayHiSentence(content);
            NLogger.d(TAG, "3、输入完成事件");
            RootShell.keyevent(66);
            isHorView = true;
            isClickSeatch = false;
        }  else if (isHorView && (mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals("android.support.v7.widget.RecyclerView")
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals("android.support.v7.widget.RecyclerView"))) {
            NLogger.d(TAG, "4、搜索结果页");
            //input swipe X1 Y1 X2 Y2
            RootShell.swipe(450,110,180,110);
           // execShellCmd("input swipe 450 110 180 110");//滑动
            sleepTimedDefault();
            isHorView = false;
            isSearchHomeList = true;
        } else if (isSearchHomeList && mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals("android.widget.HorizontalScrollView")) {
            NLogger.d(TAG, "5、滑动寻找 小组");
           // Onclic_shell("396", "110");
            RootShell.click(396,110);
            isSearchHomeList = false;
            isGroupJoinList = true;
        } else if (isGroupJoinList && (mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals("android.support.v7.widget.RecyclerView")
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals("android.support.v7.widget.RecyclerView"))) {
            NLogger.d(TAG, "6、进入小组列表");
            beginReqAdd();
        } else if (isClickJoinGroupDetailJoin && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.katana.activity.ImmersiveActivity")) {
            NLogger.d(TAG, "7、2 输入验证  不输入直接点击返回");
            look = true;
            currentAddCount--;
            RootShell.keyevent(4);
        } else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.5yU")) {
            NLogger.d(TAG, "8、离开此页");
            tagTextClick(FBStringUtil.getString(StringConst.TEAM_LKCY));
            isClickJoinGroupDetailJoin = false;
        } else if (isGroupJoinList && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.katana.activity.ImmersiveActivity")) {
            look = false;
            beginReqAdd();
        }
    }

    private void beginReqAdd() {
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, "android.support.v7.widget.RecyclerView");
                if (scrllorNodeInfos.size() > 0) {
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if (scrllorNodeInfo == null) {
                    //  ToastUtil.showToast(AppContext.getInstance().getContext(),"scrllorNodeInfo listView null");
                    return;
                }
                for (int q = 0; q < scrllorNodeInfo.getChildCount(); q++) {

                    if (currentAddCount == maxAddCount) {
                        currentAddCount = 0;
                        mRecordMap.clear();
                        stopTask();
                        break;
                    }
                    if(!look){
                        AccessibilityNodeInfo nodeInfo = scrllorNodeInfo.getChild(q);
                        if (nodeInfo.getChildCount() > 0) {
                            try {
                                AccessibilityNodeInfo nameNode = nodeInfo.getChild(0).getChild(0).getChild(0);
                                AccessibilityNodeInfo joinButtonNode = nameNode.getChild(0);
                                String name = nameNode.getContentDescription().toString();
                                if (!mRecordMap.containsKey(name)) {
                                    Log.d(TAG, " name:" + name);
                                    if (joinButtonNode.getContentDescription().toString().equals(FBStringUtil.getString(StringConst.TEAM_JRXZQQAN))) {
                                        isClickJoinGroupDetailJoin = tagTextClickShell(joinButtonNode);
                                        if (isClickJoinGroupDetailJoin) {
                                            mRecordMap.put(name, true);
                                            currentAddCount++;
                                            sleepTimedDefault();
                                        }
                                    }
                                    NLogger.d(TAG, "点击 加入 flag:" + isClickJoinGroupDetailJoin);
                                }
                            } catch (Exception e) {

                            }
                        }

                        if (q == scrllorNodeInfo.getChildCount() - 1) {
                            if (scrllorNodeInfo != null) {
                                NodeUtil.performScroller(scrllorNodeInfo);
                                break;
                            }
                        }
                    }

                }
            }
        }
    }



    public void setParms(int maxAddCount, int minAddTime, int maxAddTime, String content, int type) {
        // stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        this.content = content;
        this.runType = type;

        this.initFbMain = false;
        isClickSeatch = false;
        isSearchHomeList = false;

        isClickSeatch = false;
        isSearchHomeList = false; //是否进入了搜索主页列表
        isGroupJoinList = false;
        isClickJoinGroupDetailJoin = false;
        isHorView = false;
        look = false;
        mRecordMap.clear();
    }
}
